﻿using PhotoKiosk.Model.Device;
using PhotoKiosk.Properties;
using PhotoKiosk.ViewModel.Helpers;
using System.Resources;
using System.Windows.Input;

namespace PhotoKiosk.ViewModel
{
    class SelectSourceViewModel : ViewModelBase
    {
        private DelegateCommand m_dokCommand;
        private DelegateCommand m_cdRomCommand;
        private DelegateCommand m_sampleFolderCommand;
        private MainWindowViewModel m_mainWindowViewModel;
        private ResourceManager m_manager = new ResourceManager(typeof(Resources));

        public SelectSourceViewModel(MainWindowViewModel i_mainWindowViewModel)
        {
            m_mainWindowViewModel = i_mainWindowViewModel;
            m_mainWindowViewModel.SystemData.StartNewOrder();
        }

        public ICommand DokCommand
        {
            get
            {
                if (m_dokCommand == null)
                {
                    m_dokCommand = new DelegateCommand(param => this.dokCommandExecute(), param => this.dokCommandCanExecute());
                }
                return m_dokCommand;
            }
        }

        private bool dokCommandCanExecute()
        {
            return true;
        }

        private void dokCommandExecute()
        {
            if (Dok.IsConnected())
            {
                m_mainWindowViewModel.SystemData.SetConnectedDevice(new Dok());
                m_mainWindowViewModel.CurrentViewModel = new OrderDetailsViewModel(m_mainWindowViewModel);
            }
            else
            {
                Error = m_manager.GetString("Text_Error_Dok_Is_Not_Connected_To_The_Computer"); 
            }
        }

        public ICommand CdRomCommand
        {
            get
            {
                if (m_cdRomCommand == null)
                {
                    m_cdRomCommand = new DelegateCommand(param => this.cdRomCommandExecute(), param => this.cdRomCommandCanExecute());
                }
                return m_cdRomCommand;
            }
        }

        private bool cdRomCommandCanExecute()
        {
            return CDRom.IsExist();
        }

        private void cdRomCommandExecute()
        {
            if (CDRom.IsConnected())
            {
                m_mainWindowViewModel.SystemData.SetConnectedDevice(new CDRom());
                m_mainWindowViewModel.CurrentViewModel = new OrderDetailsViewModel(m_mainWindowViewModel);
            }
            else
            {
                Error = m_manager.GetString("Text_Error_Cdrom_Is_Not_Inserted_To_The_Computer"); 
            }
        }

        public ICommand SampleFolderCommand
        {
            get
            {
                if (m_sampleFolderCommand == null)
                {
                    m_sampleFolderCommand = new DelegateCommand(param => this.sampleFolderCommandExecute(), param => this.sampleFolderCommandCanExecute());
                }
                return m_sampleFolderCommand;
            }
        }

        private bool sampleFolderCommandCanExecute()
        {
            return true;
        }

        private void sampleFolderCommandExecute()
        {
            m_mainWindowViewModel.SystemData.SetConnectedDevice(new SampleFolder());
            m_mainWindowViewModel.CurrentViewModel = new OrderDetailsViewModel(m_mainWindowViewModel);
        }

        private string m_error = string.Empty;
        public string Error
        {
            get
            {
                return m_error;
            }
            set
            {
                m_error = value;
                OnPropertyChanged("Error");
            }
        }
    }
}
